package com.meitianhui.convenitenbuy.wxapi;


import android.content.Intent;
import android.os.Bundle;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.model.PayResultBean;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends BaseToolbarActivity implements IWXAPIEventHandler {


    private IWXAPI api;
    private String productType;


    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected int setView() {
        return R.layout.activity_pay_result;
    }

    @Override
    protected void onRefreshData() {

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {


    }

    @Override
    public void onResp(BaseResp resp) {


        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                RxBus.get().post(new PayResultBean(Constants.PAY_RESULT_SUCCESS));//支付成功
                RxBus.get().post(new PayResultBean(Constants.PAY_FIGHT_GROUP_SUCCESS));//拼团领支付成功
                RxBus.get().post(new EventBean(Constants.EVENT_TYPE_REFRESH));//钱包刷新
            } else if (resp.errCode == -2) {
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_cancel), 3);
                //如果用户取消支付 就跳到订单页面
                RxBus.get().post(new PayResultBean(Constants.PAY_RESULT_FAIL_HUIGUO));

            } else if (resp.errCode == -1) {
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_fail));
                //如果用户取消支付 就跳到订单页面
                RxBus.get().post(new PayResultBean(Constants.PAY_RESULT_FAIL_HUIGUO));

                finish();
            }
            finish();
        }
        finish();
    }
}